package com.lzj.demo.design.strategy.fzw;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

@Component
public class CouponFactory implements ApplicationContextAware {
    private Map<CouponEnum,ICoupon> couponMap = new ConcurrentHashMap<>();

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        Map<String,ICoupon> map = applicationContext.getBeansOfType(ICoupon.class);
        map.values().forEach(coupon->couponMap.put(coupon.getCoupon(),coupon));
    }

    public BigDecimal useCoupon(CouponEnum couponEnum,BigDecimal skuPrice){
        ICoupon coupon = couponMap.get(couponEnum);
        return coupon.discountAccount(skuPrice);
    }
}
